package com.example.hotelmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hotelmanager.entity.BookMsg;
import com.example.hotelmanager.entity.CheckIn;
import com.example.hotelmanager.entity.Cost;
import com.example.hotelmanager.entity.Guest;
import com.example.hotelmanager.entity.vo.GuestMsg;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.service.*;
import com.example.hotelmanager.utils.JwtUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/28 21:46
 */
@RestController
@Slf4j
@CrossOrigin
public class GuestController {
    @Autowired
    private GuestService guestService;
    @Autowired
    private JwtUtill jwtUtill;
    @Autowired
    private BackgroundService backgroundService;
    @Autowired
    private FrontService frontService;
    @Autowired
    private BookMsgService bookMsgService;
    @Autowired
    private CheckInService checkInService;
    @GetMapping("/getAllGuest")
    public Response getAllGuest(HttpServletRequest request){
        //检查是否是合法用户(可封装成service里的函数)
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            List<Guest> guests = guestService.list();
            List<GuestMsg> guestMsgs = new ArrayList<>();
            if(guests != null && guests.size()>0){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                for(Guest guest:guests){
                    //查询预定房间信息和入住信息将信息封装成guestMsg
                    LambdaQueryWrapper<BookMsg> queryWrapper=new LambdaQueryWrapper();
                    queryWrapper.eq(BookMsg::getGuestIdCard,guest.getIdCard());
                    LambdaQueryWrapper<CheckIn> queryWrapper1=new LambdaQueryWrapper();
                    queryWrapper1.eq(CheckIn::getGuestIdCard,guest.getIdCard());

                    List<BookMsg> bookMsgs = bookMsgService.list(queryWrapper);
                    List<CheckIn> checkIns = checkInService.list(queryWrapper1);
                    GuestMsg guestMsg = new GuestMsg(guest.getIdCard(),guest.getName(),guest.getContact(),-1,"");
                    if(checkIns != null && checkIns.size()>0 && checkIns.get(0).getState() == 1){
                        guestMsg.setState(1);
                        guestMsg.setRoomId(checkIns.get(0).getRoomId());
                    }else if(bookMsgs != null && bookMsgs.size()>0 && bookMsgs.get(0).getState() != 11){
                        guestMsg.setState(0);
                        guestMsg.setRoomId(bookMsgs.get(0).getResultRoom());
                    }else if(checkIns != null && checkIns.size()>0 && checkIns.get(0).getState() == 0){
                        guestMsg.setState(-1);
                        guestMsg.setRoomId(checkIns.get(0).getRoomId());
                    }
                    guestMsgs.add(guestMsg);
                }
                resultMap.put("guestMsgs",guestMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    //http://localhost:8081/deleteGuest?idCard=111
    @DeleteMapping("/deleteGuest")
    public Response deleteGuest(String idCard,HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            guestService.deleteGuest(idCard);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    @PostMapping("/addGuest")
    public Response addGuest(HttpServletRequest request, Guest guest){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            guestService.saveOrUpdate(guest);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    /**
     * 条件查询(模糊)
     * @param request
     * @param idCard
     * @return
     */
    @GetMapping("/getGuestByIdCard")
    public Response getGuestByIdCard(HttpServletRequest request,String idCard){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            List<GuestMsg> guestMsgs=guestService.getGuestByIdCard(idCard);
            if(guestMsgs!=null){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("guestMsgs",guestMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @GetMapping("/getGuestByContact")
    public Response getGuestByContact(HttpServletRequest request,String contact){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            List<GuestMsg> guestMsgs=guestService.getGuestByContact(contact);
            if(guestMsgs!=null){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("guestMsgs",guestMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @GetMapping("/getGuestByName")
    public Response getGuestByName(HttpServletRequest request,String name){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null || frontService.getById(num) != null){
            List<GuestMsg> guestMsgs=guestService.getGuestByName(name);
            if(guestMsgs!=null){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("guestMsgs",guestMsgs);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }


}
